import {API} from "../utils/request";

export function getAnnouncementsAPI() {
    return API({
        url: '/announcements/getAnnouncements',
        method: 'GET'
    })
}

export function sendCodeAPI(data) {
    return API({
        url: '/wx/message/sendCode?templekey=' + data,
        method: 'POST',
        data: data
    })
}


//主动询问订阅消息
export function onNotifyAsk() {
    // let tempId = this.data.tempIds;
    wx.requestSubscribeMessage({
        tmplIds: ['V5CRlOCJqUvAKAfWJ3MMUCu7lJTeWYAD2CHnNo_DgIQ'],
        success: res => {
            console.log('调起成功');
            if (res['V5CRlOCJqUvAKAfWJ3MMUCu7lJTeWYAD2CHnNo_DgIQ'] === 'accept') {
                console.log('允许');
                // callback('accept')
                const data = sendCodeAPI('V5CRlOCJqUvAKAfWJ3MMUCu7lJTeWYAD2CHnNo_DgIQ').then(res => {
                    console.log('data', res)
                    return res
                })
            }
            if (res['V5CRlOCJqUvAKAfWJ3MMUCu7lJTeWYAD2CHnNo_DgIQ'] === 'reject') {
                console.log('拒绝')
                // callback('reject')
            }
        },
        fail: err => {
            // callback('error');
            console.log(err)
            if (err.errCode == 20004) {
                console.log('关闭小程序主开关')
            } else {
                console.log('订阅失败')
            }
        }
    })
}